/* $Id: log.c,v 1.3 2006/03/15 03:49:16 jfb Exp $ */
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <pthread.h>

#include <canopy/string.h>

#include "log.h"


#define CNP_LOG_CHKSZ_PERIOD		60




struct cnp_logchan {
	char   *ch_name;
};


extern char *__progname;




/*
 * syslog-related parameters
 *
 */
static int cnp_syslog_send = 0;		/* send messages to syslog? */

#if 0
static int cnp_syslog_fac = LOG_DAEMON;	/* logging facility */
#endif

#ifdef SYSLOG_REENTRANT
static struct syslog_data cnp_syslog_data;	/* for reentrant version */
#endif




/*
 * cnp_log_init()
 *
 */
int
cnp_log_init(void)
{

	return (0);
}

/*
 * cnp_log()
 *
 * Log a message through the server's logging facility.
 */
void
cnp_log(int pri, const char *fmt, ...)
{
	va_list vap;

	va_start(vap, fmt);
	cnp_vlog(pri, fmt, vap);
	va_end(vap);
}


/*
 * cnp_log()
 *
 * Log a message through the server's logging facility.
 */
void
cnp_vlog(int pri, const char *fmt, va_list vap)
{
	int ret;
	size_t len;
	char prefix[32], buf[1024], ebuf[64];
	FILE *out;

	snprintf(prefix, sizeof(prefix), "%s[%u]: ", __progname, getpid());
	if ((ret = vsnprintf(buf, sizeof(buf), fmt, vap)) == -1)
		return;

	if (pri == CNP_LOG_ERRNO) {
		snprintf(ebuf, sizeof(ebuf), ": %s", strerror(errno));
		if ((len = strlcat(buf, ebuf, sizeof(buf))) >= sizeof(buf))
			len = sizeof(buf) - 1;
	}

	/* send to standard output or error, depending on level */
	out = (pri <= CNP_LOG_NOTICE) ? stdout : stderr;

	fprintf(out, "%s%s\n", prefix, buf);

	if (cnp_syslog_send == 1) {
#ifdef SYSLOG_REENTRANT
		syslog_r(pri, &cnp_syslog_data, "%s", buf);
#else
		syslog(pri, "%s", buf);
#endif
	}
}
